<template>
  <el-form :model="user" ref="userRef" :rules="rules">
    <el-form-item prop="userName" label="用户名">
      <el-input
        v-model="user.userName"
        placeholder="4-8为英文数字下划线组成,字母开头"
      ></el-input>
    </el-form-item>
    <el-form-item prop="pwd" label="密码">
      <el-input v-model="user.pwd" type="password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="userLogin">登录</el-button>
      <el-button @click="reset">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import useUserStore from "@s/user";

const userStore = useUserStore();
const rules = {
  userName: [
    {
      required: true,
      message: "请输入用户名",
    },
    {
      pattern: /^[a-zA-Z]\w{3,7}$/,
      message: "用户名格式不正确",
    },
  ],
  pwd: [
    {
      required: true,
      message: "请输入密码",
    },
    {
      pattern: /^[a-zA-Z]\w{3,7}$/,
      message: "密码格式不正确",
    },
  ],
};
const user = reactive({
  userName: "",
  pwd: "",
});
const userRef = ref<any>(null);

const userLogin = () => {
  userRef.value.validate().then(() => {
    userStore.userLogin(user);
  });
};
const reset = () => {
  userRef.value.resetFields();
};

defineOptions({
  name: "LoginPage",
});
</script>

<style scoped></style>
